package com.liuxvru.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxvru.common.constant.TestConstants;
import com.liuxvru.common.utils.SecurityUtils;
import com.liuxvru.project.domain.*;
import com.liuxvru.project.mapper.FlawMapper;
import com.liuxvru.project.mapper.TestMapper;
import com.liuxvru.project.mapper.TestPlanMapper;
import com.liuxvru.project.mapper.WorkMapper;
import com.liuxvru.project.service.ITestService;
import com.liuxvru.project.service.ITestStepService;
import com.liuxvru.project.vo.TestAllInitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 测试Service业务层处理
 *
 * @author liuxvru
 * @date 2024-11-22
 */
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {

    @Autowired
    TestPlanMapper testPlanMapper;
    @Autowired
    ITestStepService testStepService;
    @Autowired
    FlawMapper flawMapper;
    @Autowired
    WorkMapper workMapper;
    /**
     * 查询测试列表
     *
     * @param test 测试
     * @return 测试
     */
    @Override
    public List<Test> selectTestList(Test test) {
        return baseMapper.selectTestList(test);
    }

    @Override
    @Transactional
    public int add(Long testPlanId, Test test) {
        TestPlan testPlan = testPlanMapper.selectById(testPlanId);
        if (testPlan == null) {
            return 0;
        }
        test.setOutcome(TestConstants.TEST_OUTCOME_UNTESTED);
        test.setTestPlanId(testPlanId);
        test.setProjectId(testPlan.getProjectId());
        test.setRegisterWorkingHours(0L);
        test.setUpdatedBy(SecurityUtils.getUserId());
        test.setCreatedBy(SecurityUtils.getUserId());
        test.setStatus(TestConstants.TEST_OUTCOME_INITIAL);
        return baseMapper.insert(test);
    }

    @Override
    public int edit(Test test) {
        test.setUpdatedBy(SecurityUtils.getUserId());
        return baseMapper.updateById(test);
    }

    @Override
    @Transactional
    public int editTestAndTestStepVo(TestAllInitVo testAllInitVo) {
        testAllInitVo.getTest().setUpdatedBy(SecurityUtils.getUserId());
        this.baseMapper.updateById(testAllInitVo.getTest());
        if(testAllInitVo.getTestSteps() != null){
            testStepService.updateBatchById(testAllInitVo.getTestSteps());
        }
        return 1;
    }

    @Override
    @Transactional
    public int delTests(List<Long> testIds) {
        this.removeByIds(testIds);
        testStepService.remove(new LambdaQueryWrapper<TestStep>().in(TestStep::getTestId, testIds));
        flawMapper.delete(new LambdaQueryWrapper<Flaw>().in(Flaw::getTestId, testIds));
        return 1;
    }

    @Override
    public TestAllInitVo init(Long testId) {
        Test test = this.baseMapper.selectById(testId);
        if (test == null) throw new RuntimeException("测试不存在");
        Work work = workMapper.selectById(test.getWorkItemId());
        if (work == null) throw new RuntimeException("工作项不存在");
        List<TestStep> testSteps = testStepService.list(new LambdaQueryWrapper<TestStep>().eq(TestStep::getTestId, testId));
        List<Flaw> flaws = flawMapper.selectList(new LambdaQueryWrapper<Flaw>().eq(Flaw::getTestId, testId));
        TestAllInitVo testAllInitVo = new TestAllInitVo();
        testAllInitVo.setTest(test);
        testAllInitVo.setWork(work);
        testAllInitVo.setTestSteps(testSteps);
        testAllInitVo.setFlaws(flaws);
        return testAllInitVo;
    }

}
